package eu.liquify.ui {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import no.doomsday.console.ConsoleUtil;
	public class GridIndicator extends UIItem {
		
		public var gridWidth:Number = 10;
		public var gridHeight:Number = 10;
		public var fitToGrid:Boolean = true;
		
		private var data:BitmapData;
		private var display:Bitmap;
		
		public function GridIndicator() {
			display = new Bitmap();
			addChild(display);
		}
		
		public function setData(newData:BitmapData):void {
			if (data) data.dispose();
			data = newData;
			display.bitmapData = data;
			gridWidth = data.width;
			gridHeight = data.height;
			resize();
		}
		
		override public function resize():void {
			super.resize();
			
			if (!data || data.width != gridWidth || data.height != gridHeight) {
				if (data) data.dispose();
				data = new BitmapData(gridWidth, gridHeight, true, 0x00000000);
				display.bitmapData = data;
			}
			
			if (fitToGrid) {
				width = int(width/gridWidth)*gridWidth;
				height = int(height/gridHeight)*gridHeight;
			}
			
			if (gridWidth/gridHeight > width/height) {
				display.scaleX = display.scaleY = width/gridWidth;
			} else {
				display.scaleX = display.scaleY = height/gridHeight;
			}
			/*
			data.setPixel32(0, 0, 0xFF00FF00);
			data.setPixel32(data.width-1, 0, 0xFF00FF00);
			data.setPixel32(0, data.height-1, 0xFF00FF00);
			data.setPixel32(data.width-1, data.height-1, 0xFF00FF00);
			//*/
		}
		
		public function draw(x:int, y:int, color:uint):void {
			data.setPixel32(x, y, color);
		}
		
	}

}